Code coverage | 您所在的位置:网站首页 › pycharm run with coverage › Code coverage |
Code coverage Code coverage in IntelliJ IDEA lets you analyze which lines of code were executed during a particular run. It helps determine the share of code covered by tests and identify areas that lack sufficient test coverage. Enable the Code Coverage for Java pluginThis functionality relies on the Code Coverage for Java plugin, which is bundled and enabled in IntelliJ IDEA by default. If the relevant features aren't available, make sure that you didn't disable the plugin. Press Ctrl+Alt+S to open the IDE settings and then select Plugins. Open the Installed tab, find the Code Coverage for Java plugin, and select the checkbox next to the plugin name. Run with coverageThe entry points for running coverage analysis are the same as you would normally use to run an application: For main method definitions, click Run in the editor gutter, then select Run with Coverage For run configurations, click More Actions in the Run widget, then select Run with Coverage For build tool tasks, depending on your build tool, go to Maven or Gradle tool window. Right-click the task and select Run with Coverage Coverage analysis executes the corresponding run configuration with the coverage agent attached. This agent instruments the bytecode to keep track of the execution line-by-line. After the execution completes, the results of the analysis appear in the IDE. Coverage suitesThe collection of coverage data for a specific run is called a coverage suite. IntelliJ IDEA can display the results for one or more coverage suites at once. If multiple suites are selected, the IDE displays merged results from these suites. This means that a line is considered covered if it was executed in at least one of the selected suites. The corresponding suite files are located in the IntelliJ IDEA's system directory: %LOCALAPPDATA%\JetBrains\IntelliJIdea2024.1\coverage ~/.cache/JetBrains/IntelliJIdea2024.1/coverage ~/Library/Caches/JetBrains/IntelliJIdea2024.1/coverage Manage suitesIn the main menu, go to Run | Manage Coverage Reports Ctrl+Alt+F6. In the Choose Coverage Suite to Display menu: Use the checkboxes to select active suites. Active suites define the coverage data that is currently displayed in the IDE. Use Add to import external suite file, for example, generated in the CI server or sent to you by someone else. You can use .ic (IntelliJ IDEA runner) and .exec (JaCoCo runner) files. To view an imported suite collected with IntelliJ IDEA runner, you do not have to compile or run the project. Use Remove to remove a suite from the list but keep the file in the storage. Use Delete to remove a suite from the list and delete the file from the storage. Read coverage dataIntelliJ IDEA shows coverage results in: Coverage tool window Project tool window Editor Code coverage is only supported for the code that belongs to your current project. Project tool windowThe Project tool window displays: the percentage of the covered classes and lines for directories the percentage of the covered methods and lines for classes Coverage tool windowThe report breaks down the project by coverage percentage of classes, methods, lines, and branches. Branch coverage shows the percentage of the executed branches in statements like if-else or switch. It is available for the JaCoCo runner and for the IntelliJ IDEA runner with the Branch Coverage option enabled. The Coverage tool window has the following options: Item Description Select whether to show packages hierarchically or flatten the structure to show all of them on the same level. If this option is on, IntelliJ IDEA automatically opens the selected item in the editor. Otherwise, you need to double-click items to open them. If this option is on, IntelliJ IDEA automatically locates in the tool window the files that you open in the editor. Generate a code coverage report and save it to the specified directory. For more information, refer to Generate coverage report. Import a coverage suite from disk. Filter coverage results. You can choose to view only the classes with uncommitted changes to focus on recent updates or hide the classes that are fully covered with tests. EditorIn the editor gutter, lines are highlighted according to their coverage status: Green – lines that have been executed Red – lines that haven't been executed Yellow – lines that were executed partially, like when only one branch of an if-else statement is visited To view statistics for a line of code, click the corresponding color indicator in the gutter. A popup that opens shows the number of hits and condition evaluation statistics (only for IntelliJ IDEA runner). Additionally, the statistics popup has the following options: For JUnit tests: open the test that covers a line in a separate dialog. To be able to use this feature, make sure you ran coverage analysis with the Branch Coverage and Track per test coverage options Open the bytecode of the current class in a separate dialog Change the colors of the coverage indicators in the gutter Hide coverage dataDo one of the following: Close the tab with coverage statistics in the Coverage tool window (View | Tool Windows | Coverage). Click coverage highlighting in the gutter and select Hide coverage. Generate coverage reportGo to Run | Generate Coverage Report, or click in the Coverage tool window. This option only becomes available after you run the necessary tests with coverage. In the Generate Coverage Report dialog, specify the directory to store the generated report and optionally select the Open generated HTML in browser checkbox. Configure code coverageIn IntelliJ IDEA, there are coverage settings that apply to individual run configurations, and those applicable per-project. Run configuration settingsFor each run configuration, you can specify the scope of the coverage analysis. Configure classes and packagesGo to Run | Edit Configurations and click the run configuration on the left panel. Click Modify options, then select the following options under Code Coverage: Specify classes and packages: select this option to configure classes and packages for which you want to see the coverage information. Exclude classes and packages: select this option to configure classes and packages that you want to exclude from coverage. Project settingsFor projects, you can configure the following options: Show options before applying coverage to the editor Ask whether to make the recently collected coverage suite active every time you run coverage analysis. In this case a dialog will appear, where you can choose to only display the new results, to add them to the existing results, or save them to storage without viewing. Do not apply collected coverage Only save the recently collected coverage suite to storage after you have run coverage analysis. In this case, new coverage data will not be shown unless you explicitly select the corresponding suite in Run | Manage Coverage Reports. Replace active suites with the new one Hide the currently shown coverage suites and use the newly collected one after you have run coverage analysis. In this case, only the results from the most recent coverage run will be shown. Add to the active suites Add the newly collected coverage suite to the list of active suites after you have run coverage analysis. This keeps the existing coverage results and adds the new results on top of it. Activate Coverage View Open the Coverage tool window every time you run coverage analysis. Choose coverage runner Select the coverage runner: IntelliJ IDEA or JaCoCo Branch coverage only for IntelliJ IDEA runner Collect granular coverage data for if and switch statements' branches. Track per test coverage only for IntelliJ IDEA runner Lets you trace back to specific tests, during which a line was executed. To navigate to these tests, click the coverage highlighting in the editor gutter, then click Show Tests Covering Line. Collect coverage in test folders only for IntelliJ IDEA runner Collect code coverage statistics for tests. By default, coverage data only shows which lines were covered in source roots. Use this option to enable coverage collection in test roots as well. Ignore implicitly declared default constructors only for IntelliJ IDEA runner Exclude the implicit default constructors from coverage statistics. No-arg constructors that are declared explicitly will remain included. Exclude annotations only for IntelliJ IDEA runner Exclude elements marked with the specified annotations from coverage results, for example, Lombok's @Generated elements. Change colors of the coverage highlightingPress Ctrl+Alt+S to open the IDE settings and then select Editor | Color Scheme | General. In the list of components, expand the Line Coverage node and select a type of coverage: for example, Full, Partial or Uncovered. Click the Foreground field to open the Select Color dialog. Select a color, apply the changes, and close the dialog. Last modified: 04 April 2024Test-driven developmentGenerate test data |
CopyRight 2018-2019 实验室设备网 版权所有 |